package com.ruoyi.jobservice.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SProvinceCityEntity;
import com.ruoyi.system.service.ISProvinceCityService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jobservice.domain.SCompany;
import com.ruoyi.jobservice.service.ISCompanyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用工单位信息Controller
 * 
 * @author jeffery
 * @date 2021-08-31
 */
@RestController
@RequestMapping("/jobservice/company")
public class SCompanyController extends BaseController
{
    @Autowired
    private ISCompanyService sCompanyService;

    @Autowired
    private ISProvinceCityService isProvinceCityService;

    @Autowired
    private ISysUserService userService;
    /**
     * 查询用工单位信息列表
     */
    @PreAuthorize("@ss.hasPermi('jobservice:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(SCompany sCompany)
    {
        startPage();
        List<SCompany> list = sCompanyService.selectSCompanyList(sCompany);
        return getDataTable(list);
    }

    /**
     * 导出用工单位信息列表
     */
    @PreAuthorize("@ss.hasPermi('jobservice:company:export')")
    @Log(title = "用工单位信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SCompany sCompany)
    {
        List<SCompany> list = sCompanyService.selectSCompanyList(sCompany);
        ExcelUtil<SCompany> util = new ExcelUtil<SCompany>(SCompany.class);
        return util.exportExcel(list, "用工单位信息数据");
    }

    /**
     * 获取用工单位信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('jobservice:company:query')")
    @GetMapping(value = "/{companyId}")
    public AjaxResult getInfo(@PathVariable("companyId") Long companyId)
    {
        return AjaxResult.success(sCompanyService.selectSCompanyByCompanyId(companyId));
    }

    /**
     * 新增用工单位信息
     */
    @PreAuthorize("@ss.hasPermi('jobservice:company:add')")
    @Log(title = "用工单位信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SCompany sCompany)
    {
        return toAjax(sCompanyService.insertSCompany(sCompany));
    }

    @PreAuthorize("@ss.hasPermi('jobservice:company:add')")
    @Log(title = "用工单位账号注册", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addCompanyUser")
    public AjaxResult addCompanyUser(@RequestBody SCompany sCompany)
    {
        if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(sCompany.getUserName())))
        {
        return AjaxResult.error("新增用户'" + sCompany.getUserName() + "'失败，登录账号已存在");
        }
        sCompany.setCreateName(getUsername());
        return toAjax(sCompanyService.insertSCompanyUser(sCompany));
    }

    /**
     * 修改用工单位信息
     */
    @PreAuthorize("@ss.hasPermi('jobservice:company:edit')")
    @Log(title = "用工单位信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SCompany sCompany)
    {
        return toAjax(sCompanyService.updateSCompany(sCompany));
    }

    /**
     * 删除用工单位信息
     */
    @PreAuthorize("@ss.hasPermi('jobservice:company:remove')")
    @Log(title = "用工单位信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{companyIds}")
    public AjaxResult remove(@PathVariable Long[] companyIds)
    {
        return toAjax(sCompanyService.deleteSCompanyByCompanyIds(companyIds));
    }


    @GetMapping(value = "/getAllCompanysInfo")
    public AjaxResult getAllCompanysInfo()
    {
        List<SCompany> list = sCompanyService.getAllCompanysInfo();
        return AjaxResult.success(list);
    }

    @GetMapping(value = "/getAllProvince")
    public AjaxResult getAllProvince()
    {
        QueryWrapper<SProvinceCityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("p_code",0);
        return AjaxResult.success(isProvinceCityService.list(wrapper));
    }

    @GetMapping(value = "/getChildArea/{pCode}")
    public AjaxResult getChildArea(@PathVariable("pCode") Integer pCode)
    {
        QueryWrapper<SProvinceCityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("p_code",pCode);
        return AjaxResult.success(isProvinceCityService.list(wrapper));
    }

}
